package com.zeratul.common.mail.config;

import com.zeratul.common.pool.utils.ThreadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;

import static com.zeratul.common.core.constant.PoolConstants.MAIL_POOL;

/**
 * 邮件发送线程池配置
 */
@Slf4j
@Configuration
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true")
public class MailThreadPoolConfig {

    /**
     * CPU核心数
     */
    private final int core = Runtime.getRuntime().availableProcessors();

    /**
     * 队列大小
     */
    private final int poolSize = 10000;

    /**
     * 邮件线程池配置
     */
    @Bean(name = MAIL_POOL)
    public Executor mailExecutorService() {
        //创建线程池
        return ThreadUtils.newThreadPool(core * 2,
                core * 4,
                60, poolSize,
                "mail-pool",
                true);
    }
}